package controllers;

import model.forview.AdministrationModelForView;
import model.forview.UserManagerModelForView;
import model.wrap.user.User;
import model.wrap.user.UserException;
import play.mvc.Controller;
import play.mvc.Result;

public class Administration extends Controller
{

    public static Result getPageOption()
    {
	return null;// ok(index.render("Your new application is ready."));
    }

    public static Result administration()
    {
	return ok(views.html.administration.render(new AdministrationModelForView()));
    }

    public static Result getPageUserManager(String userName)
    {
	// TODO проверка
	try
	{
	    UserManagerModelForView modelForView = new UserManagerModelForView(new User(userName));
	    return ok(views.html.userManager.render(modelForView));
	}
	catch (UserException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return badRequest();
	}
    }

    public static Result userManager()
    {
	UserManagerModelForView modelForView = new UserManagerModelForView(form().bindFromRequest().data());
	try
	{
	    User user = new User(modelForView.getUserName());
	    user.setLastName(modelForView.getLastName());
	    user.setFirstName(modelForView.getFirstName());
	    user.setUserType(modelForView.getUserType());
	    user.update();
	}
	catch (UserException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return badRequest();
	}
	return null;
    }

    public static Result getUserpic(String user)
    {
	try
	{
	    User usr = new User(user);
	    response().setContentType("img/jpg");
	    response().setHeader("Content-Disposition", "attachment; filename=" + String.valueOf(usr.getEmail().hashCode()) + ".jpg");
	    return ok(usr.getUserpic());
	}
	catch (Exception e)
	{
	    return redirect("/assets/img/userpic.jpg");
	}
    }

}
